package com.wanhu.player
{
	import com.adobe.serialization.json.*;

	import flash.events.*;
	import flash.events.Event;
	import flash.net.*;
	import flash.utils.*;

	public class Reader
	{
		private var _f:Function;
		private var _p:Object;
		private var _loaded:*;
		private var l:URLLoader;
		private var r:URLRequest;

		public function Reader(url:String, v:URLVariables, f:Function, p:Object=null)
		{
			this._f=f;
			this._p=p;
			this.l=new URLLoader();
			this.r=new URLRequest();
			this.r.method=URLRequestMethod.POST;
			this.r.url=url;
			this.r.data=v;
			this.l.addEventListener(Event.COMPLETE, this.createJson);
			this.l.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorListener);
			this.l.load(this.r);
			return;
		} // end function

		private function ioErrorListener(event:IOErrorEvent):void
		{
			this._f(null, this._p);
			return;
		} // end function

		protected function createJson(event:Event):void
		{
			var e:*=event;
			this.l.removeEventListener(Event.COMPLETE, this.createJson);
			try
			{
				this._loaded=com.adobe.serialization.json.JSON.decode(String(e.target.data));
			}
			catch (e:TypeError)
			{
				_f(null, _p);
			}
			this._f(this._loaded, this._p);
			return;
		} // end function

	}
}
